using iOSClub.WebAPI.Models.Users;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;


namespace iOSClub.WebAPI.Controllers.Member
{
    [Authorize(Roles = "President")]
    [TokenActionFilter]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PresidentController : ControllerBase
    {
        private readonly MemberContext _context;
        private readonly IHttpContextAccessor _httpContextAccessor;

        public PresidentController(MemberContext context,IHttpContextAccessor httpContextAccessor)

        {
            _context = context;
            _httpContextAccessor = httpContextAccessor;
        }

        // GET: api/Member
        [HttpGet]
        public async Task<ActionResult<IEnumerable<MemberModel>>> GetMembers()
        {
            if (_context.Students == null! || !await MemberModelExists())
            {
                return NotFound();
            }

            return await _context.Students.ToListAsync();
        }

        
        // GET: api/Member/5
        
        [HttpGet("{id}")]
        public async Task<ActionResult<MemberModel>> GetMember(string id)
        {
            if (_context.Students == null! || !await MemberModelExists())
            {
                return NotFound();
            }

            var memberModel = await _context.Students.FindAsync(id);

            if (memberModel == null)
            {
                return NotFound();
            }

            return memberModel;
        }

        [HttpGet]
        public async Task<ActionResult<IEnumerable<MemberModel>>> GetCadre()
        {
            if (_context.Students == null! || !await MemberModelExists())
            {
                return NotFound();
            }

            return await _context.Students.Where(x => x.Identity != "Member").ToListAsync();
        }

        [HttpPost]
        public async Task<ActionResult> Update([FromBody] MemberModel model)
        {
            if (_context.Students == null! || !await MemberModelExists())
            {
                return NotFound();
            }
            
            _context.Entry(model).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                var a = await _context.Students.AnyAsync(x => x.Equals(model));
                if (!a)
                {
                    return NotFound();
                }
                throw;
            }

            return NoContent();
        }

        private async Task<bool> MemberModelExists()
        {
            var member = _httpContextAccessor.HttpContext?.User.GetUser();
            if (member is null) return false;
            return await _context.Students?.AnyAsync(x =>
                x.Id == member.Id && x.UserName == member.UserName && x.Identity == member.Identity)!;
        }

    }
}
